<?php

namespace app\apixcx\controller;

use app\admin\model\Info as InfoModel;
use app\admin\model\User as UserModel;
use app\admin\model\Card as CardModel;
use app\admin\model\Relation as RelationModel;

class Index
{
    protected $CardModel;
    protected $UserModel;
    protected $InfoModel;
    protected $RelationModel;

    public function __construct()
    {
        $this->CardModel = new CardModel();
        $this->UserModel = new UserModel();
        $this->InfoModel = new InfoModel();
        $this->RelationModel = new RelationModel();
    }

    /**
     * info 首页接口
     * uid 当前用户ID.
     */
    public function info($uid, $page = 1)
    {
        $token = getheaders()['Http-Token'];
        if (!$token || $token != cache('token'.$uid)) {
            return -100;
        }
        // $app_id = 'wx878a12a5118620ac';
        $app_id = getheaders()['App-Id'];
        $InfoRes = $this->InfoModel->where('wechatappid', $app_id)->find();
        $InfoRes->hidden(['wechatappid', 'wechatsecret'])->toArray();
        $gid = $InfoRes['id'];

        $cardlst = [];
        $query = [
            'ispage' => true,
            'limit' => 10,
            'where' => [
                ['uid', '=', $uid],
                ['type', 'neq', '-1'],
                ['gid', '=', $gid],
                ['type', 'neq', '2'],
            ],
            'order' => [
                'update_time' => 'desc',
            ],
        ];
        $RelationRes = $this->RelationModel->getLst($query);

        foreach ($RelationRes as $k => $v) {
            $card = $this->CardModel->get($v['cid']);
            $card['type'] = $v['type'];
            if ($card) {
                $cardlst[] = $card;
            }
        }

        foreach ($cardlst as $k => $v) {
            if ($v['gid'] != $gid) {
                unset($cardlst[$k]);
            }
        }

        $cardlstDefaultArray = [];
        $defaultQuery = [
            'where' => [
                ['is_top', '=', 1],
                ['is_disabled', 'neq', 0],
                ['gid', '=', $gid],
            ],
            'order' => [
                'update_time' => 'desc',
            ],
        ];
        $typeData = [
            'id' => 2,
            'text' => '系统默认',
        ];
        $cardlstDefault = $this->CardModel->getLst($defaultQuery);

        foreach ($cardlstDefault as $k => $v) {
            $v['type'] = $typeData;
            $cardlstDefaultArray[] = $v;
        }

        $data = [
            'data' => array_values($cardlst),
            'datadefault' => $cardlstDefaultArray,
            'info' => $InfoRes,
        ];

        return $data;
    }

    public function upload()
    {
        $token = getheaders()['Http-Token'];
        if (!$token || $token != cache('token'.$uid)) {
            return -100;
        }

        $fileinfo = $_FILES['file'];
        $file = request()->file('file');
        $info = $file->move(env('ROOT_PATH').'public/uploads');
        print_r($info->getSaveName());
    }
}
